package org.redmagic.controller;

import java.util.List;
import java.util.Set;

import org.bson.types.ObjectId;
import org.redmagic.api.UiService;
import org.redmagic.domain.Tags;
import org.redmagic.ui.UiProperty;
import org.redmagic.ui.UiPropertyListResponse;
import org.redmagic.ui.UiPropertyResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/ui")
public class UiController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(UiController.class);

	@Autowired
	private UiService magicUiService;

	@RequestMapping(value = "/getAllTags", method = RequestMethod.GET)
	@ResponseBody
	public Tags getAllTags() {
		LOGGER.info("Getting all tags");
		
		return magicUiService.getAllTags();
	}

	@RequestMapping(value = "/getSelectedTags/{key}", method = RequestMethod.GET)
	@ResponseBody
	public Tags getSelectedTags(@PathVariable String key) {
		LOGGER.info("Getting selected tags for property: {}", key);
		
		return magicUiService.getSelectedTags(key);
	}

	/* Table data load - This loads the data for the table */
	@RequestMapping(value = "/getAllProperties", method = RequestMethod.POST)
	@ResponseBody
	public UiPropertyListResponse getAllProperties() {
		LOGGER.info("Getting all properties");
		try {
			List<UiProperty> properties = magicUiService.listProperties();
			LOGGER.info("Found {} properties", properties.size());
			return UiPropertyListResponse.getSuccessResponse(properties);
		} catch (Exception e) {
			e.printStackTrace();
			return UiPropertyListResponse.getFailedResponse(e.getMessage());
		}
	}

	/* CRUD operation - Add the property */
	@RequestMapping(value = "/addProperty", method = RequestMethod.POST)
	@ResponseBody
	public UiPropertyResponse insertGroup(@RequestParam String name, @RequestParam Set<String> tags, 
			@RequestParam String value, @RequestParam String group) {
		
		UiProperty property = new UiProperty(ObjectId.get().toString(), name, tags, value, group);
		
		LOGGER.info("Creating property: {}", property);
		UiPropertyResponse jsonJtableResponse;

		try {
			magicUiService.createProperty(property);
			jsonJtableResponse = UiPropertyResponse.getSuccessResponse(property);
		} catch (Exception e) {
			e.printStackTrace();
			jsonJtableResponse = UiPropertyResponse.getFailedResponse(e.getMessage());
		}
		return jsonJtableResponse;
	}
	
	/* CRUD operation - Update the property */
	@RequestMapping(value = "/updateProperty", method = RequestMethod.POST)
	@ResponseBody
	public UiPropertyResponse updateRole(@RequestParam String key, @RequestParam String name, @RequestParam Set<String> tags, 
			@RequestParam String value, @RequestParam String group) {
		
		UiProperty property = new UiProperty(key, name, tags, value, group);
		
		LOGGER.info("Updating property: {}", property);
		UiPropertyResponse jsonJtableResponse;
		try {
			magicUiService.updateProperty(property);
			jsonJtableResponse = UiPropertyResponse.getSuccessResponse(property);
		} catch (Exception e) {
			jsonJtableResponse = UiPropertyResponse.getFailedResponse(e.getMessage());
		}
		return jsonJtableResponse;
	}

	/* CRUD operation - Delete the property */
	@RequestMapping(value = "/deleteProperty", method = RequestMethod.POST)
	@ResponseBody
	public UiPropertyResponse delete(@RequestParam String key) {
		LOGGER.info("Deleting property: {}", key);
		UiPropertyResponse jsonJtableResponse;
		
		try {
			magicUiService.removeProperty(key);
			jsonJtableResponse = UiPropertyResponse.getSuccessResponse(null);
		} catch (Exception e) {
			jsonJtableResponse = UiPropertyResponse.getFailedResponse(e.getMessage());
		}
		return jsonJtableResponse;
	}
}